package com.gcxy.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2024-2024, BUG闭退！
 * Author: Huang先森
 * Date: 2024/7/22 10:48
 * FileName: MyBatisPlusGenerator
 * Description: 代码生成器
 */
public class MyBatisPlusGenerator {

    /**
     * 项目路径
     */
    private static final String PARENT_DIR = System.getProperty("user.dir");

    /**
     * 基本路径
     */
    private static final String SRC_MAIN_JAVA = "/src/main/java/";

    /**
     * xml路径
     */
    private static final String XML_PATH = PARENT_DIR + "/cg-system/src/main/resources/mapper";

    /**
     * entity路径
     */
    private static final String ENTITY_PATH = PARENT_DIR + "/cg-system/src/main/java/com/gcxy/system/entity";

    /**
     * mapper路径
     */
    private static final String MAPPER_PATH = PARENT_DIR + "/cg-system/src/main/java/com/gcxy/system/mapper";


    /**
     * service路径
     */
    private static final String SERVICE_PATH = PARENT_DIR + "/cg-system/src/main/java/com/gcxy/system/service";

    /**
     * serviceImpl路径
     */
    private static final String SERVICE_IMPL_PATH = PARENT_DIR + "/cg-system/src/main/java/com/gcxy/system/service/impl/";


    /**
     * controller路径
     */
    private static final String CONTROLLER_PATH = PARENT_DIR + "/cg-web/src/main/java/gcxy/controller";

    /**
     * 数据库url
     */
    private static final String DB_URL = "jdbc:mysql://10.255.70.20:3306/weekly?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "gcxy@2024";


    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("appraise");
        tables.add("clazz");
        tables.add("course");
        tables.add("evaluate");
        tables.add("export_record");
        tables.add("agency");
        tables.add("guidance");
        tables.add("position");
        tables.add("project");
        tables.add("resource");
        tables.add("role");
        tables.add("role_resource");
        tables.add("semester");
        tables.add("student_semester");
        tables.add("student_teacher");
        tables.add("user");
        tables.add("user_role");
        tables.add("weekly");
        tables.add("weekly_project");
        tables.add("weekly_time");
        autoGenerator(tables);
    }

    private static Map<OutputFile, String> getPathInfo() {
        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.entity, ENTITY_PATH);
        pathInfo.put(OutputFile.mapper, MAPPER_PATH);
        pathInfo.put(OutputFile.service, SERVICE_PATH);
        pathInfo.put(OutputFile.serviceImpl, SERVICE_IMPL_PATH);
        pathInfo.put(OutputFile.controller, CONTROLLER_PATH);
        pathInfo.put(OutputFile.mapperXml, XML_PATH);
        return pathInfo;
    }

    private static void autoGenerator(List<String> tables) {
        //获取项目路径
        String projectPath = System.getProperty("user.dir");
        //java下的文件路径
        String filePath = projectPath + "/cg-system/src/main/java";
        FastAutoGenerator.create(DB_URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("Huang先森") // 设置作者
                            //需要添加swagger依赖并配置,开启swagger
                            .enableSwagger()
                            //时间策略
                            .dateType(DateType.TIME_PACK)
                            //格式化时间格式
                            .commentDate("yyyy-MM-dd")
                            //禁止打开输出目录，默认false
                            .disableOpenDir()
                            //覆盖生成文件
                            .fileOverride()
                            .outputDir(filePath);
                })
                .packageConfig(builder -> {

                    builder.parent("")
                            .xml("mappers")
                            .entity("com.gcxy.system.entity")
                            .mapper("com.gcxy.system.mapper")
                            .service("com.gcxy.system.service")
                            .serviceImpl("com.gcxy.system.service.impl")
                            .controller("com.gcxy.controller")
                            .pathInfo(getPathInfo());
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables); // 设置需要生成的表名
//                            .addTablePrefix("b_", "t_"); // 设置过滤表前缀
                    builder.entityBuilder()
                            .enableLombok()
                            .enableRemoveIsPrefix()
                            .idType(IdType.AUTO)
                            .build();
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                })
                .templateConfig(builder -> {
                    //builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICEIMPL);
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
